/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.umum.common;

/**
 *
 * @author Administrator
 */
import java.io.*;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 网页海盗类
 * @version 1.0
 *
 */
public class Thief {

    private String value_;
    private String src_;
    private String encode_;
    private boolean isGet = false;
    private String errText;



    public Thief() {
    }

    public Thief(String surl,String encode) {
        this.src_ = surl;
        this.encode_=encode;
    }
    /**
     * 设置访问代理
     * @param host
     * @param port
     */
    public void setProxy(String host,String port){
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", port);
        System.out.println("set proxy successed");
    }
    /**
     * 开始窃取内容
     * @throws IOException
     */
    @SuppressWarnings("empty-statement")
    public boolean thief(){
        System.out.println("test log");
        if (src_==null) {errText="\u8BF7\u5148\u8BBE\u7F6E\u6E90URL\u5730\u5740.";return false;}
        if(encode_==null){encode_="iso8859_1";}
        value_ ="";
        try {
            URL fileur;
            fileur = new URL(src_);
            InputStream filecon = null;
            DataInputStream filedata = null;
            StringBuffer contentdata = new StringBuffer();
            String fileline="";
            try {
                filecon = fileur.openStream();
                filedata = new DataInputStream(filecon);
                while ((fileline = filedata.readLine()) != null) {
                    contentdata.append(fileline+"\n");
                }
                value_ = new String(contentdata.toString().getBytes("iso8859_1"),encode_);
                contentdata = null;
                isGet = true;
                filedata.close();
                filecon.close();
            }catch (IOException e) {
                e.printStackTrace();
                this.setErrText("\u4E0D\u80FD\u6B63\u786E\u8BFB\u53D6\u9875\u9762\u5185\u5BB9.");
            }
        }catch(MalformedURLException e){
            this.setErrText("\u4F60\u8F93\u5165\u7684URL\u5730\u5740\u4E0D\u5B58\u5728.");
            return false;
        }
        return true;
    }

    /**
     * 截取首尾字符串之间的内容
     * @param head
     * @param foot
     */
    public void cut(String head,String foot){
        if (isGet==false) thief();
        int i,j,len;
        i = value_.indexOf(head);
        j = value_.indexOf(foot);
        len = head.length();
        if ((i >=0) && (j>=0)){
            value_ = value_.substring(i + len);
            value_ = value_.substring(0,j -i - len -1);
        }else{
            this.value_ ="\u51FD\u6570cut\u6307\u5B9A\u88C1\u51CF\u5185\u5BB9\u4E0D\u5B58\u5728\2C\u8BF7\u91CD\u65B0\u5B9A\u4E49";
        }
    }

    /**
     * 清除一些回车空格等特殊字符
     */
    public void clearReturn(){
        if (isGet==false) thief();
        clearSpec("\n");
    }
    public void clearSpace(){
        if (isGet==false) thief();
        clearSpec(" | ");
    }
    private void clearSpec(String regEx){
        Pattern p=Pattern.compile(regEx);
        Matcher m=p.matcher(value_); //标签匹配
        value_ = m.replaceAll("");
    }

    /**
     * 过滤字符串，以首尾字符之间的换成新值
     * @param head
     * @param foot
     * @param str
     */
    public void filtBy(String head,String foot,String str){
        if (isGet==false) thief();
        String regEx = head+".+"+foot;
        Pattern p=Pattern.compile(regEx);
        Matcher m=p.matcher(value_); //标签匹配
        value_ = m.replaceAll(str);
    }
    //--------------------
    public String getUrl() {
        return src_;
    }

    public void setUrl(String _url) {
        this.src_ = _url;
    }

    public String getErrText() {
        return errText;
    }

    public String getValue() {
        return value_;
    }

    private void setErrText(String errText) {
        this.errText = errText;
    }
}

